package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.common.entity.VeBaseDepartment;
import org.edu.modules.common.mapper.VeBaseDepartmentMapper;
import org.edu.modules.common.mapper.VeBaseTeacherMapper;
import org.edu.modules.common.service.IVeBaseDepartmentService;
import org.edu.modules.common.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther 李少君
 * @Date 2021-12-13 16:24
 */
@Service
public class VeBaseDepartmentServiceImpl extends ServiceImpl<VeBaseDepartmentMapper, VeBaseDepartment> implements IVeBaseDepartmentService {

    @Autowired
    private VeBaseDepartmentMapper veBaseDepartmentMapper;

    @Autowired
    private VeBaseTeacherMapper veBaseTeacherMapper;

    @Override
    public List<VeBaseDepartment> getTreeList() {
        List<VeBaseDepartment> rootList = this.veBaseDepartmentMapper.getRootList();
        List<VeBaseDepartment> bodyList = this.veBaseDepartmentMapper.getBodyList();
        rootList = setJlnyName(rootList);
        if (bodyList != null && !bodyList.isEmpty()) {
            Map<Integer, Integer> map = new HashMap<>();
            rootList.forEach(department -> getChild(department, map));
            return rootList;
        }
        return null;
    }

    @Override
    public List<VeBaseDepartment> getdeptListByPid(Integer pid, String jgh, String jgmc) {
        return veBaseDepartmentMapper.getdeptListByPid(pid, jgh, jgmc);
    }

    @Override
    public List<VeBaseDepartment> getDepartmentAndTeacherList() {
        List<VeBaseDepartment> rootList = this.veBaseDepartmentMapper.getRootList();
        List<VeBaseDepartment> bodyList = this.veBaseDepartmentMapper.getBodyList();
        rootList = setJlnyName(rootList);
        if (bodyList != null && !bodyList.isEmpty()) {
            Map<Integer, Integer> map = new HashMap<>();
            rootList.forEach(department -> getChildAndTeacher(department, map));
            return rootList;
        }
        return null;
    }

    @Override
    public VeBaseDepartment getDepartmentByName(Integer id, String name) {
        VeBaseDepartment veBaseDepartment = this.veBaseDepartmentMapper.getDepartmentByName(id, name);
        if (veBaseDepartment != null)
            if (veBaseDepartment.getJlny() != null) {
                veBaseDepartment.setJlnyName(DateTimeUtil.timestampToDate(veBaseDepartment.getJlny().intValue()));
            } else {
                veBaseDepartment.setJlnyName("");
            }
        return veBaseDepartment;
    }

    @Override
    public VeBaseDepartment getDepartmentByCode(Integer id, String code) {
        VeBaseDepartment veBaseDepartment = this.veBaseDepartmentMapper.getDepartmentByCode(id, code);
        if (veBaseDepartment != null)
            if (veBaseDepartment.getJlny() != null) {
                veBaseDepartment.setJlnyName(DateTimeUtil.timestampToDate(veBaseDepartment.getJlny().intValue()));
            } else {
                veBaseDepartment.setJlnyName("");
            }
        return veBaseDepartment;
    }

    public void getChild(VeBaseDepartment department, Map<Integer, Integer> map) {
        List<VeBaseDepartment> bodyList = this.veBaseDepartmentMapper.getBodyList();
        List<VeBaseDepartment> childList = new ArrayList<>();
        bodyList = setJlnyName(bodyList);
        bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> c.getPid().equals(department.getId()))
                .forEach(c -> {
                    map.put(c.getId(), c.getPid());
                    getChild(c, map);
                    childList.add(c);
                });
        department.setChildren(childList);
    }

    private List<VeBaseDepartment> setJlnyName(List<VeBaseDepartment> list) {
        if (list.size() > 0)
            for (VeBaseDepartment model : list) {
                if (model.getJlny() != null) {
                    model.setJlnyName(DateTimeUtil.timestampToDate(model.getJlny().intValue()));
                    continue;
                }
                model.setJlnyName("");
            }
        return list;
    }

    public void getChildAndTeacher(VeBaseDepartment department, Map<Integer, Integer> map) {
        List<VeBaseDepartment> bodyList = this.veBaseDepartmentMapper.getBodyList();
        List<VeBaseDepartment> childList = new ArrayList<>();
        bodyList = setJlnyName(bodyList);
        bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> c.getPid().equals(department.getId()))
                .forEach(c -> {
                    map.put(c.getId(), c.getPid());
                    getChild(c, map);
                    c.setTeacherList(this.veBaseTeacherMapper.getTeacherListByDepId(c.getId()));
                    childList.add(c);
                });
        department.setChildren(childList);
    }
}
